/**
 * Copyright (C), 2024, xxxx有限公司
 * FileName: IntegerTypeExample
 * Author:   TLVM
 * Date:     3/23/2024 12:27 PM
 * Description: INT类型和Integer类型
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.iqcctt.simpleflow.typeengine.example;

import com.iqcctt.simpleflow.annotation.SimpleFiledFlow;
import com.iqcctt.simpleflow.exception.SimpleFlowException;
import com.iqcctt.simpleflow.typeengine.TypeInterface;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.Optional;

/**
 * 〈一句话功能简述〉<br>
 * 〈INT类型和Integer类型〉
 *  注意事项int暂不支持指定默认值
 * @author TLVM
 * @create 3/23/2024
 * @since 1.0.0
 */
public class IntegerTypeExample extends ToolsExtends implements TypeInterface {

    @Override
    public void load(Object result, Field field, SimpleFiledFlow simpleFiledFlow, int msg) throws NoSuchFieldException, IllegalAccessException {
        if(getValue(result, field.getName())==null&&StringUtils.isNotBlank(simpleFiledFlow.defValue())){
            setFieldValue(result,field.getName(),Integer.valueOf(simpleFiledFlow.defValue()));
        }else if(getValue(result, field.getName())==null){
            throw new SimpleFlowException((msg==0?"全局控制":"局部控制")+"当前节点指定字段不能为空，字段"+field.getName());
        }
        if(simpleFiledFlow.lenMin()!=-1||simpleFiledFlow.lenMin()!=0){
            Integer value = (Integer) getValue(result,field.getName());
            if(value<simpleFiledFlow.lenMin()){
                throw new SimpleFlowException((msg==0?"全局控制":"局部控制")+"当前节点字段异常,字段："+field.getName()+"默认长度："+simpleFiledFlow.lenMin());
            }
        }
    }
}
